package com.hz.filemanager.ui.clean;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.hz.filemanager.R;
import com.hz.filemanager.adapter.ScanAdapter;
import com.hz.filemanager.model.FileDataManager;
import com.hz.filemanager.ui.base.InterruptActivity;
import com.hz.filemanager.utils.StorageQueryUtil;
import com.hz.filemanager.utils.ThreadManager;


public class ScanActivity extends InterruptActivity {

    private LottieAnimationView av;
    private Toolbar toolbar;
    private TextView scoreTv;
    private TextView perTv;
    private RecyclerView listViewRv;
    private ScanAdapter scanAdapter;
    float scanSize = 0;
    private final static int SCANING = 1;
    private final static int SCANED = 2;

    //扫描一项的时间，默认1秒
    private int scanTime = 1000;

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SCANING:
                    if(isQuit) return;

                    int index = msg.arg1;
                    scanAdapter.getDatas().get(index).setScan(true);
                    scanAdapter.notifyItemChanged(index,1);

                    break;
                case SCANED:
                    if(isQuit) return;

                    switch (type){
                        case TYPE_CLEAN:
                            Intent intent = new Intent(ScanActivity.this, CleanScanResultActivity.class);
                            intent.putExtra(CleanScanResultActivity.TYPE, CleanScanResultActivity.TYPE_CLEAN);
                            intent.putExtra(CleanScanResultActivity.ALLSIZE, (int)scanSize);
                            startActivity(intent);
                            break;
                        case TYPE_BOOST:
                            Intent boostIntent = new Intent(ScanActivity.this, ScanResultActivity.class);
                            boostIntent.putExtra(ScanResultActivity.TYPE, ScanResultActivity.TYPE_BOOST);
                            boostIntent.putExtra(ScanResultActivity.NUMBER, scoreTv.getText());
                            startActivity(boostIntent);
                            break;
                        case TYPE_COOLER:
                            Intent cooler = new Intent(ScanActivity.this, ScanResultActivity.class);
                            cooler.putExtra(ScanResultActivity.TYPE, ScanResultActivity.TYPE_COOLER);
                            cooler.putExtra(ScanResultActivity.NUMBER, scoreTv.getText());
                            startActivity(cooler);
                            break;
                        case TYPE_BATTERY:
                            Intent battery = new Intent(ScanActivity.this, ScanResultActivity.class);
                            battery.putExtra(ScanResultActivity.TYPE, ScanResultActivity.TYPE_BATTERY);
                            battery.putExtra(ScanResultActivity.NUMBER, scoreTv.getText());
                            startActivity(battery);
                            break;
                    }

                    finish();
                    break;
            }
        }
    };


    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan;
    }


    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        scoreTv = findViewById(R.id.score_tv);
        listViewRv = findViewById(R.id.listview_rv);
        perTv = findViewById(R.id.per_tv);
    }

    @Override
    protected void initEvent() {

        type = getIntent().getIntExtra(TYPE, 0);
        scanAdapter = new ScanAdapter(ScanAdapter.TYPE_SCAN);

        switch (type){
            case TYPE_CLEAN:
                toolbar.setTitle(R.string.junk_clean);
                perTv.setText(R.string.mb);
                scanAdapter.setListener(new ScanAdapter.OnItemScanCompleted() {
                    @Override
                    public void scanComplete(int position, float scansize) {
                        scanSize = scanSize + scansize;
                        scoreTv.setText(String.valueOf((int) scanSize));
                    }
                });
                scanAdapter.setDatas(FileDataManager.getInstance().scanCleanDatas());
                break;
            case TYPE_BOOST:
                toolbar.setTitle(R.string.phone_boost);
                scoreTv.setText(String.valueOf(StorageQueryUtil.getRAMPercent(getApplicationContext())));
                perTv.setText("%");
                scanTime = 3000;
                scanAdapter.setListener(new ScanAdapter.OnItemScanCompleted() {
                    @Override
                    public void scanComplete(int position, float scansize) {

                    }
                });
                scanAdapter.setDatas(FileDataManager.getInstance().scanBoostDatas());
                break;
            case TYPE_COOLER:

                toolbar.setTitle(R.string.phone_cooler);
                scoreTv.setText(FileDataManager.getInstance().getCurrentTemperature());
                perTv.setText("℃");
                scanTime = 3000;
                scanAdapter.setListener(new ScanAdapter.OnItemScanCompleted() {
                    @Override
                    public void scanComplete(int position, float scansize) {

                    }
                });
                scanAdapter.setDatas(FileDataManager.getInstance().scanBoostDatas());
                break;
        }

        listViewRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        listViewRv.setAdapter(scanAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();

        ThreadManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                for(int i=0; i<scanAdapter.getItemCount(); i++){

                    Message msg = handler.obtainMessage();
                    msg.what=1;
                    msg.arg1 = i;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler.sendEmptyMessage(SCANED);
            }
        });
    }

    @Override
    protected void onDestroy() {
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);//清空消息队列
        }
        super.onDestroy();
    }
}
